1 package attr;
2
3 import java.lang.*;
4 import java.util.*;
5 import java.sql.*;
6 import javax.swing.table.*;
7 import java.awt.*;
8 import java.text.*;
9 import attr.*;
10 import activity.*;
11 import javax.swing.*;

12
13 public
class Product {
14     
private String productId;
15     
private String productName;
16     
private double price;
17     
private int quantity;
18     
public static String[] columnNames = {"PID", "Name", "Price", "AvailableQuantity"};
19     
20     
public Product() {}
21     
public Product(String productId) {
22         
if (!productId.isEmpty())
23             
this.productId = productId;
24         
else
25             
throw new IllegalArgumentException("Fill in the ID");
26     }
27     
28     
public void setProductName(String name) {
29         
if (!name.isEmpty())
30             
this.productName = name;
31         
else
32             
throw new IllegalArgumentException("Fill in the name");
33     }
34     
public void setPrice(double p) {
35         
this.price = p;
36     }
37     
public void setQuantity(int q) {
38         
this.quantity = q;
39     }
40     
public String getProductId() {
41         
return productId;
42     }
43     
public String getProductName() {
44         
return productName;
45     }
46     
public double getPrice() {
47         
return price;
48     }
49     
public int getQuantity() {
50         
return quantity;
51     }
52     
53     
public void fetch() {
54         String query =
"SELECT `productId`, `productName`, `price`, `quantity` FROM `product` WHERE productId='"+this.productId+"';";
55         Connection con =
null;
56         Statement st =
null;
57         ResultSet rs =
null;
58         System.
out.println(query);
59         
try {
60             Class.forName(
"com.mysql.jdbc.Driver");
61             System.
out.println("driver loaded");
62             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
63             System.
out.println("connection done");//connection with database established
64             st = con.createStatement();
//create statement
65             System.
out.println("statement created");
66             rs = st.executeQuery(query);
//getting result
67             System.
out.println("results received");
68             
69             
while(rs.next()) {
70                 
this.productName = rs.getString("productName");
71                 
this.price = rs.getDouble("price");
72                 
this.quantity = rs.getInt("quantity");
73             }
74         }
75         
catch(Exception ex) {
76             System.
out.println("Exception : " +ex.getMessage());
77         }
78         
finally {
79             
try {
80                 
if(rs!=null)
81                     rs.close();
82
83                 
if(st!=null)
84                     st.close();
85
86                 
if(con!=null)
87                     con.close();
88             }
89             
catch(Exception ex) {}
90         }
91     }
92     
93     
public void sellProduct(String uid, int amount) {
94         String date =
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
95         String query =
"INSERT INTO `purchaseInfo` (`userId`, `productId`, `amount`, `date`, `cost`) VALUES ('"+uid+"','"+this.productId+"',"+amount+", '"+date+"', "+(amount*this.price)+");";
96         Connection con =
null;
97         Statement st =
null;
98         System.
out.println(query);
99         
try {
100             Class.forName(
"com.mysql.jdbc.Driver");
101             System.
out.println("driver loaded");
102             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
103             System.
out.println("connection done");//connection with database established
104             st = con.createStatement();
//create statement
105             System.
out.println("statement created");
106             st.execute(query);
//insert
107             System.
out.println("data inserted");
108             updateProduct(
this.productName, this.price, this.quantity-amount);
109         }
110         
catch(Exception ex) {
111             JOptionPane.showMessageDialog(
null,"Customer doesn't exist!");
112             System.
out.println("Exception : " +ex.getMessage());
113         }
114         
finally {
115             
try {
116                 
if(st!=null)
117                     st.close();
118
119                 
if(con!=null)
120                     con.close();
121             }
122             
catch(Exception ex) {}
123         }
124     }
125     
126     
public void updateProduct(String name, double price, int quantity) {
127         String query =
"UPDATE `product` SET `productName`='"+name+"', `price`="+price+", `quantity`="+quantity+" WHERE `productId`='"+this.productId+"';";
128         Connection con =
null;
129         Statement st =
null;
130         System.
out.println(query);
131         
try {
132             Class.forName(
"com.mysql.jdbc.Driver");
133             System.
out.println("driver loaded");
134             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
135             System.
out.println("connection done");//connection with database established
136             st = con.createStatement();
//create statement
137             System.
out.println("statement created");
138             st.executeUpdate(query);
//insert
139             System.
out.println("data inserted");
140             JOptionPane.showMessageDialog(
null,"Done!");
141         }
142         
catch(Exception ex) {
143             JOptionPane.showMessageDialog(
null,"Failed!");
144             System.
out.println("Exception : " +ex.getMessage());
145         }
146         
finally {
147             
try {
148                 
if(st!=null)
149                     st.close();
150
151                 
if(con!=null)
152                     con.close();
153             }
154             
catch(Exception ex) {}
155         }
156     }
157     
158     
public void createProduct() {
159         String query =
"INSERT INTO `product` (`productName`, `price`, `quantity`) VALUES ('"+productName+"','"+price+"','"+quantity+"');";
160         Connection con =
null;
161         Statement st =
null;
162         System.
out.println(query);
163         
try {
164             Class.forName(
"com.mysql.jdbc.Driver");
165             System.
out.println("driver loaded");
166             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
167             System.
out.println("connection done");//connection with database established
168             st = con.createStatement();
//create statement
169             System.
out.println("statement created");
170             st.execute(query);
//insert
171             System.
out.println("data inserted");
172             JOptionPane.showMessageDialog(
null,"Product Created!");
173         }
174         
catch(Exception ex) {
175             JOptionPane.showMessageDialog(
null,"Failed to add Product!");
176             System.
out.println("Exception : " +ex.getMessage());
177         }
178         
finally {
179             
try {
180                 
if(st!=null)
181                     st.close();
182
183                 
if(con!=null)
184                     con.close();
185             }
186             
catch(Exception ex) {}
187         }
188     }
189     
190     
public static DefaultTableModel searchProduct(String keyword, String byWhat) {
191         DefaultTableModel model =
new DefaultTableModel();
192         model.setColumnIdentifiers(columnNames);
193         String query =
"SELECT `productId`, `productName`, `price`, `quantity` FROM `product` WHERE `productId`='"+keyword+"';";
194         
if (byWhat.equals("By Name"))
195             query =
"SELECT `productId`, `productName`, `price`, `quantity` FROM `product` WHERE `productName` LIKE '%"+keyword+"%';";
196         
else {}
197         Connection con =
null;
198         Statement st =
null;
199         ResultSet rs =
null;
200         System.
out.println(query);
201         
try {
202             Class.forName(
"com.mysql.jdbc.Driver");
203             System.
out.println("driver loaded");
204             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
205             System.
out.println("connection done");//connection with database established
206             st = con.createStatement();
//create statement
207             System.
out.println("statement created");
208             rs = st.executeQuery(query);
//getting result
209             System.
out.println("results received");
210             
211             
while(rs.next()) {
212                 model.addRow(
new Object[]{rs.getString("productId"), rs.getString("productName"), rs.getDouble("price"), rs.getInt("quantity")});
213             }
214         }
215         
catch(Exception ex) {
216             System.
out.println("Exception : " +ex.getMessage());
217         }
218         
finally {
219             
try {
220                 
if(rs!=null)
221                     rs.close();
222
223                 
if(st!=null)
224                     st.close();
225
226                 
if(con!=null)
227                     con.close();
228             }
229             
catch(Exception ex) {}
230         }
231         
return model;
232     }
233     
234     
public void deleteProduct() {
235         String query1 =
"DELETE FROM `product` WHERE `productId`='"+this.productId+"';";
236         Connection con =
null;
237         Statement st =
null;
238         System.
out.println(query1);
239         
try {
240             Class.forName(
"com.mysql.jdbc.Driver");
241             System.
out.println("driver loaded");
242             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
243             System.
out.println("connection done");//connection with database established
244             st = con.createStatement();
//create statement
245             System.
out.println("statement created");
246             st.execute(query1);
//delete
247             System.
out.println("data deleted");
248             JOptionPane.showMessageDialog(
null,"Product Deleted!");
249         }
250         
catch(Exception ex) {
251             JOptionPane.showMessageDialog(
null,"Failed to delete product!");
252             System.
out.println("Exception : " +ex.getMessage());
253         }
254         
finally {
255             
try {
256                 
if(st!=null)
257                     st.close();
258
259                 
if(con!=null)
260                     con.close();
261             }
262             
catch(Exception ex) {}
263         }
264     }
265 }


Gõ tìm kiếm nhanh...